//
// Forward declarations of functions
//
var handleHomePage;
var handleChallengePage;
var handlePasswordPage;
var handleAccountsPage;
var handleStatementsAndDocumentsPage;
var handleDownloadStatementPage;




function isYearExpanded(rows, year)
{
    // Assume the year is already expanded until proven otherwise.
    // The 80% use case is that the year will already be expanded.
    var isExpanded = true;
    
    // Get the row number for the year
    var idxThisYear = findRowIndexContaining(rows, year);
    
    // Get the row number for the previous year
    var idxPrevYear = findRowIndexContaining(rows, (year - 1));
    if (0 > idxPrevYear)
    {
        idxPrevYear = (rows.length - 1);
    }
    
    // If the difference between row numbers is 2 then the year is collapsed
    var difference = idxPrevYear - idxThisYear;
    if (2 >= difference)
    {
        isExpanded = false;
    }

    return isExpanded;
}

function toggleYearFolding(rows, year)
{
    // Get the row number for the year
    var idxThisYear = findRowIndexContaining(rows, year);
    
    // Get the row number for the previous year
    var idxPrevYear = findRowIndexContaining(rows, (year - 1));
    if (0 > idxPrevYear)
    {
        idxPrevYear = (rows.length - 1);
    }
    
    // Expand or collapse the year
    var btnExpander = rows[idxThisYear].getElementsByTagName("input")[0];
    if ('undefined' != btnExpander)
    {
        // This will cause the page to reload which means this
        // page will need to be reparsed so just return with a -1
        btnExpander.click();
    }
}

function getRowsForYear(rows, year)
{
    var retVal;
    
    // Get the row number for the year
    var idxThisYear = findRowIndexContaining(rows, year);
    
    // Get the row number for the previous year
    var idxPrevYear = findRowIndexContaining(rows, (year - 1));
    if (0 > idxPrevYear)
    {
        idxPrevYear = (rows.length - 1);
    }
    
    // Get the rows for the months of this year
    retVal = rows.slice(idxThisYear, (idxPrevYear - 1));
    
    return retVal;
}

function onYearExpandCompleted(page, arrRows, month, year)
{
    var arrMonthRows = getRowsForYear(arrRows, year);
    assert((undefined != arrMonthRows), ("Unable to get rows for year " + year));
    
    var idxMonth = findRowIndexContaining(arrMonthRows, month);
    assert((0 <= idxMonth), ("Unable to get row index for month " + month));
    
    var rowMonth = arrMonthRows[idxMonth];
    assert((undefined != rowMonth), ("Unable to get row for month " + month));
    
    var arrCells = nodeListToArray(rowMonth.cells);
    
    var idxCellDownload = findRowIndexContaining(arrCells, "Download PDF");
    assert((0 <= idxCellDownload), ("Unable to get cell index for cell containing 'Download PDF'"));
    
    var nlAnchors = arrCells[idxCellDownload].getElementsByTagName("a");
    assert((undefined != nlAnchors), ("Unable to get anchors for cell containing 'Download PDF'"));
    
    // There should be just one anchor in the cell
    assert((1 == nlAnchors.length), "Download PDF cell contains more anchors than expected");
    
    var anchor = nlAnchors[0];
    if (validateControls(page.moniker, [anchor]))
    {
        // This will load a page that will contain the link to download the PDF
        document.location = anchor.href;
    }
}

function mainAutomation()
{
    var pageRecognized = false;
    
    for (var index = 0; index < PAGES.length; index++)
    {
        if (null != document.location.href.match(PAGES[index].href))
        {
            pageRecognized = true;
            PAGES[index].handler(PAGES[index]);
        }
    }

    if (!pageRecognized)
    {
        logError("ERROR - Unknown page:\n\n" + document.location.href);
    }
}

mainAutomation();
